using HCore.Unity.Core;
using HCore.Unity.Extension;
using UnityEngine;

namespace HCore.Tests.Runtime.HeatMapGrid
{
    public class TestHeatMapGrid : MonoBehaviour
    {
        private const int _WIDTH = 70;
        private const int _HEIGHT = 70;
        [SerializeField] private HeatMapVisual HeatMapVisual;
        private Grid2D<HeatMapGridObject> mGrid;

        private void Awake()
        {
            mGrid = new Grid2D<HeatMapGridObject>(_WIDTH, _HEIGHT, (self, x, y) => new HeatMapGridObject(self, x, y), Vector2.zero, 5f);
        }

        private void Start()
        {
            var pos = GameUtils.MainCamera.transform.position;
            pos.x = _WIDTH;
            pos.y = _HEIGHT;
            GameUtils.MainCamera.transform.localPosition = pos;

            HeatMapVisual.SetGrid(mGrid);
        }


        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                var position = GameUtils.GetWorldPositionFromScreen().ToVector2();
                AddValue(position, 100, 5, 20);
            }
        }

        private void AddValue(Vector2 worldPosition, int value, int fullValueRange, int totalRange)
        {
            var lowerValueAmount = Mathf.RoundToInt((float)value / (totalRange - fullValueRange)); // 计算低值
            mGrid.GetCellXY(worldPosition, out var originX, out var originY);
            // 我们通过循环，获取四个三角形，来组成一个菱形
            for (var x = 0; x < totalRange; x++)
            for (var y = 0; y < totalRange - x; y++)
            {
                var radius = x + y;
                var addValueAmount = value;
                if (radius > fullValueRange) addValueAmount -= lowerValueAmount * (radius - fullValueRange);

                mGrid.GetCell(originX + x, originY + y)?.AddValue(addValueAmount); // 右上角 三角形 
                if (x != 0) mGrid.GetCell(originX - x, originY + y)?.AddValue(addValueAmount); // 左上角 三角形

                if (y != 0)
                {
                    mGrid.GetCell(originX + x, originY - y)?.AddValue(addValueAmount); // 右下角 三角形
                    if (x != 0) mGrid.GetCell(originX - x, originY - y)?.AddValue(addValueAmount); // 左下角 三角形
                }
            }
        }
    }

    public class HeatMapGridObject
    {
        private const int _MIN = 0;
        private const int _MAX = 100;

        private readonly Grid2D<HeatMapGridObject> mGrid;
        private readonly int mX;
        private readonly int mY;

        public int Value;

        public HeatMapGridObject(Grid2D<HeatMapGridObject> grid, int x, int y)
        {
            mGrid = grid;
            mX = x;
            mY = y;
        }

        public void AddValue(int addValue)
        {
            Value = Mathf.Clamp(Value + addValue, _MIN, _MAX);
            mGrid.TriggerCellChanged(mX, mY);
        }

        public float GetValueNormalized()
        {
            return (float)Value / _MAX;
        }

        public override string ToString()
        {
            return Value.ToString();
        }
    }
}